package com.terry.test3;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * 二叉树中序遍历输出
 * 示例
 * 输入： a{b{d,e{g,h{,l}}},c{f}}
 * 输出： dbgehiafc
 * @author 小八
 * @date 2023年02月14日9:16
 */
public class Title37 {

    public static void main(String[] args) {
        Scanner  in  = new Scanner(System.in);
        while(in.hasNextLine()){
            String line = in.nextLine();
            char[] chars = line.toCharArray();
            Deque<TreeNode> deque = new ArrayDeque<>();
            TreeNode root = new TreeNode();
            root.c = chars[0];
            deque.addLast(root);
            int i=1;
            while(i<line.length()){
                //存在左子节点
                if(chars[i] == '{' && chars[i+1] != ','){
                    i++;
                    TreeNode temp = new TreeNode();
                    temp.c = chars[i];
                    root = deque.getLast();
                    root.left = temp ;
                    deque.addLast(temp);
                    i++;
                }else if(chars[i] ==','){
                    i++;
                    deque.removeLast();
                    root = deque.getLast();
                    TreeNode temp = new TreeNode();
                    temp.c = chars[i];
                    root.right = temp ;
                    deque.addLast(temp);
                    i++;
                }else if(chars[i] == '{' && chars[i+1] == ','){ //左子结点为空的情况
                    //只是占个空位
                    deque.addLast(root);
                    i= i+1;
                }else if(chars[i]=='}'){
                    deque.removeLast();
                    i++;
                }
            }
            TreeNode first = deque.getFirst();
            printTree(first);
            System.out.println();
        }
    }
    public static void printTree( TreeNode first ){
        if(first == null){
            return ;
        }
        printTree(first.left);
        System.out.print(first.c);
        printTree(first.right);
    }
}
